<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
/**
 * 文章内页
 */
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class ArticleController extends HomebaseController {
    //文章内页
    public function index() {
    	$id=intval($_GET['id']);
    	$article=sp_sql_post($id,'');
        $post_id='posts'.$article['object_id'];

        sp_add_user_action($post_id);



        // $ucode=$_POST['usercode'];

         // $usercode=$_POST['usercode'];
        $article_id_auth=$article['object_id']; 


        // $posts_model_auth=M("Posts")->where("id"=>$article_id_auth)->find();
        // $post_auth=$posts_model_auth->where("id"=>$article_id_auth)->find();
    	if(empty($article)){
    	    header('HTTP/1.1 404 Not Found');
    	    header('Status:404 Not Found');
    	    if(sp_template_file_exists(MODULE_NAME."/404")){
    	        $this->display(":404");
    	    }
    	    
    	    return ;
    	}



      
        // $_SESSION['surl']=$_SERVER['REQUEST_URI'];

       // if(empty($ucode)){
       //      $this->error("您没有观看视频的权限！");
       //  }

                

  

        // if($usercode !== '8888'){

        //  // <a href="{:leuu('article/index',array('id'=>$vo['tid'],'cid'=>$vo['term_id']))}">{$vo.post_excerpt|msubstr=0,256}</a>
        //  // $this->success("正在跳转中",leuu('article/index',array('id'=>$vo['tid'],'cid'=>$vo['term_id'])));
        //  $this->error("您没有观看视频的权限！");
        // return ;
        // }

         
        $count = sp_get_actionlogs_count($post_id,"*","view");
        

        $termid=$article['term_id'];
        $term_obj= M("Terms");
        $term=$term_obj->where("term_id='$termid'")->find();
        
        $article_id=$article['object_id'];
        
        $posts_model=M("Posts");
        $posts_model->save(array("id"=>$article_id,"post_hits"=>array("exp","post_hits+1")));
        
        $article_date=$article['post_modified'];
        
        $join = "".C('DB_PREFIX').'posts as b on a.object_id =b.id';
        $join2= "".C('DB_PREFIX').'users as c on b.post_author = c.id';
        $rs= M("TermRelationships");
        
        $next=$rs->alias("a")->join($join)->join($join2)->where(array("post_modified"=>array("egt",$article_date), "tid"=>array('neq',$id), "status"=>1,'term_id'=>$termid))->order("post_modified asc")->find();
        $prev=$rs->alias("a")->join($join)->join($join2)->where(array("post_modified"=>array("elt",$article_date), "tid"=>array('neq',$id), "status"=>1,'term_id'=>$termid))->order("post_modified desc")->find();
        
         

        $this->assign("count",$count);
        $this->assign("next",$next);
        $this->assign("prev",$prev);
        
        $smeta=json_decode($article['smeta'],true);
        $content_data=sp_content_page($article['post_content']);

        $article_summary=$article['post_excerpt'];

        $article['post_content']=$content_data['content'];


        $this->assign("page",$content_data['page']);
        $this->assign($article);
        $this->assign("smeta",$smeta);
        $this->assign("term",$term);
        $this->assign("article_summary",$article_summary);
        $this->assign("article_id",$article_id);
        
        $tplname=$term["one_tpl"];
        $tplname=sp_get_apphome_tpl($tplname, "article");
        $this->display(":$tplname");

      


    	
    }

     public function sp_do_auth(){

            return ture;
     }

    public function do_auth(){
         $id=intval($_GET['id']);
         $cid=intval($_GET['cid']);
         $usercode=$_POST['usercode'];
         $article=sp_sql_post($id,'');
         $article_id=$article['object_id'];
        
         $posts_model=M("Posts");
         $post=$posts_model->where(array("id"=>$article_id))->find();
        if(!empty($usercode) && $usercode==$post['post_code']){
                // <a href="{:leuu('article/index',array('id'=>$vo['tid'],'cid'=>$vo['term_id']))}">{$vo.post_excerpt|msubstr=0,256}</a>
                // $this->success("正在跳转中",leuu('article/index',array('id'=>$vo['tid'],'cid'=>$vo['term_id'])));usercode'=>$usercode
           $this->success("正在跳转中",leuu('article/index',array('id'=>$id,'cid'=>$cid,'usercode'=>$usercode)));
            // $this->display(":index");
          



          }else{

            $this->error("您没有观看视频的权限！");
         }
            

           

     }

    
    public function do_like(){
    	$this->check_login(); //取消用户登录
    	
    	$id=intval($_GET['id']);//posts表中id
    	
    	$posts_model=M("Posts");
    	
    	$can_like=sp_check_user_action("posts$id",2);

        /*$can_like=true;*/
    	
    	if($can_like){
    		$posts_model->save(array("id"=>$id,"post_like"=>array("exp","post_like+1")));
    		$this->success("赞好啦！");
    	}else{
    		$this->error("您已赞过啦！");
    	}
    	
    }
}
